﻿using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace DrawDotGame
{
    public class LevelScroller : MonoBehaviour
    {
        public const string JSON_PATH = "Json/LevelsData";

        private static int maxUnlockedLevel;
        public static int levelSnapped = 1;
        public const int LEVELS_PER_PACK = 6;
        public static bool isLevelDetailActive;

        public ScrollRect levelDetailScrollview;
        public ScrollRect levelPackScrollview;
        public ScrollRect levelScrollview;

        public GameObject levelDetailContent;
        public GameObject levelPackContent;
        public GameObject buttonGroupPrefab;
        public GameObject levelPackPrefab;

        public GameObject levelScrollContent;
        public GameObject levelPrefab;

        [Header("Config")]
        public Color lockedColor;
        // How many percent of levels need to be solved to unlock next pack
        public int packCompletePercentage = 80;

        private string[] data;
        private const string MAX_UNLOCKED_LEVEL_PPK = "SGLIB_MAX_UNLOCKED_LEVEL";

        // Use this for initialization
        void Start()
        {
            //Get level data
            data = null;
            string path = JSON_PATH;
            TextAsset textAsset = Resources.Load<TextAsset>(path);
            data = textAsset.ToString().Split(';');

            //Get level solved data
            string[] levelSolvedData = PlayerPrefs.GetString(LevelManager.LEVELSOLVED_KEY).Split('_');
            int highestLevelSolved;

            //Find the highest level that solved
            if (levelSolvedData.Length == 1)
            {
                highestLevelSolved = 0;
            }
            else
            {
                highestLevelSolved = int.Parse(levelSolvedData[0].Trim());
                for (int i = 1; i < levelSolvedData.Length; i++)
                {
                    if (highestLevelSolved < int.Parse(levelSolvedData[i]))
                    {
                        highestLevelSolved = int.Parse(levelSolvedData[i]);
                    }
                }
                //unlockAll
                //highestLevelSolved = 100;
            }

            //Find all levels that solved in the pack 
            //float range = highestLevelSolved / LEVELS_PER_PACK;
            //List<int> listLevelSolvedInRange = new List<int>();
            //if (highestLevelSolved != 0)
            //{
            //    foreach (string o in levelSolvedData)
            //    {
            //        if (range > 0)
            //        {
            //            if (int.Parse(o) >= range * LEVELS_PER_PACK && int.Parse(o) <= (range + 1) * LEVELS_PER_PACK)
            //            {
            //                listLevelSolvedInRange.Add(int.Parse(o));
            //            }
            //        }
            //        else
            //        {
            //            listLevelSolvedInRange.Add(int.Parse(o));
            //        }
            //    }
            //}

            //Handle level detail scrollview
            #region Handle Level Detail Scrollview   

            //Caculate level group
            float levelgroupNumber = data.Length;// Mathf.Ceil(data.Length / (float)buttonGroupPrefab.transform.childCount);
            //ScrollToSelectedPack();
            //int count = 0;

            ////Generate level group, each level group has 6 levels 
            //for (int i = 1; i <= levelgroupNumber; i++)
            //{
            //    GameObject buttonGroup = Instantiate(buttonGroupPrefab, levelDetailContent.transform.position, Quaternion.identity) as GameObject;
            //    buttonGroup.transform.SetParent(levelDetailContent.transform);
            //    buttonGroup.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);

            //    int childCount = 0;
            //    for (int j = count; j < buttonGroup.transform.childCount * i; j++)
            //    {
            //        if (j >= data.Length)
            //        {
            //            buttonGroup.transform.GetChild(childCount).gameObject.SetActive(false);
            //        }
            //        else
            //        {
            //            LevelData levelData = JsonUtility.FromJson<LevelData>(data[j]);
            //            Transform theChild = buttonGroup.transform.GetChild(childCount);
            //            theChild.GetComponentInChildren<Text>().text = levelData.levelNumber.ToString();

            //            LevelButtonController levelButtonController = theChild.GetComponent<LevelButtonController>();
            //            GameObject lockOb = theChild.Find("Lock").gameObject;
            //            GameObject imgSolvedOb = theChild.Find("imgSolved").gameObject;
            //            Image levelImg = theChild.Find("Mask").Find("Image").GetComponent<Image>();
            //            RectTransform levelImgRT = levelImg.GetComponent<RectTransform>();
            //            string file = Path.Combine(Application.persistentDataPath, levelData.levelNumber.ToString() + ".png");
            //            //If this level is solved -> show the solved screenshot
            //            if (LevelManager.IsLevelSolved(levelData.levelNumber) && File.Exists(file))
            //            {
            //                byte[] bytes = File.ReadAllBytes(file);
            //                Texture2D tex2D = new Texture2D(2, 2, TextureFormat.ARGB32, false);
            //                tex2D.LoadImage(bytes);
            //                float scaleFactor = levelImgRT.rect.height / tex2D.height;
            //                levelImg.sprite = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), Vector2.zero);
            //                levelImg.SetNativeSize();
            //                levelImg.transform.localScale = Vector3.one * scaleFactor;
            //                imgSolvedOb.gameObject.SetActive(true);
            //                lockOb.gameObject.SetActive(false);
            //                maxUnlockedLevel = levelData.levelNumber;
            //            }
            //            else
            //            {
            //                Sprite sprite = Resources.Load<Sprite>("Screenshots/" + levelData.levelNumber.ToString());
            //                float scaleFactor = levelImgRT.rect.height / sprite.rect.height;
            //                levelImg.sprite = sprite;
            //                levelImg.SetNativeSize();
            //                //levelImg.transform.localScale = Vector3.one * scaleFactor;
            //                imgSolvedOb.gameObject.SetActive(false);

            //                //Unlock all level from 0 to 6
            //                if (levelData.levelNumber <= LEVELS_PER_PACK || levelData.levelNumber <= maxUnlockedLevel)
            //                {
            //                    levelButtonController.isLocked = false;
            //                    lockOb.SetActive(false);
            //                    levelImg.color = Color.white;
            //                    maxUnlockedLevel = levelData.levelNumber;
            //                }
            //                //Unlock all levels in range
            //                else if (levelData.levelNumber <= (range + 1) * LEVELS_PER_PACK)
            //                {
            //                    levelButtonController.isLocked = false;
            //                    lockOb.SetActive(false);
            //                    levelImg.color = Color.white;
            //                    maxUnlockedLevel = levelData.levelNumber;
            //                }
            //                else //Check if reached complete percent -> unlock level
            //                {
            //                    if (levelData.levelNumber <= (range + 2) * LEVELS_PER_PACK)
            //                    {
            //                        float checkedPercent = Mathf.Ceil((packCompletePercentage * LEVELS_PER_PACK) / 100f);
            //                        if (listLevelSolvedInRange.Count >= checkedPercent)
            //                        {
            //                            levelButtonController.isLocked = false;
            //                            lockOb.SetActive(false);
            //                            levelImg.color = Color.white;
            //                            maxUnlockedLevel = levelData.levelNumber;
            //                        }
            //                        else
            //                        {
            //                            levelButtonController.isLocked = true;
            //                            levelImg.color = lockedColor;
            //                        }
            //                    }
            //                    else
            //                    {
            //                        levelButtonController.isLocked = true;
            //                        levelImg.color = lockedColor;
            //                    }
            //                }
            //            }
            //            count++;
            //        }
            //        childCount++;
            //    }
            //}

            //levelDetailScrollview.GetComponent<BetterScrollview>().DisableInvisibleItems();
            #endregion

            #region Handle Level Pack Scrollview


            //Caculate pack number
            //float packNumber = Mathf.Ceil(data.Length / (float)LEVELS_PER_PACK);
            Debug.Log("levelgroupNumber : " + levelgroupNumber);
            for (int i = 0; i < levelgroupNumber; i++)
            {
                GameObject levelPackTemp = Instantiate(levelPrefab, levelScrollview.content.position, Quaternion.identity) as GameObject;
                levelPackTemp.transform.SetParent(levelScrollContent.transform);
                levelPackTemp.transform.localScale = new Vector3(1, 1, 1);
                //LevelPackController levelPackControl = levelPackTemp.GetComponent<LevelPackController>();

                LevelData levelData = JsonUtility.FromJson<LevelData>(data[i]);
                maxUnlockedLevel = levelData.levelNumber;;

                LevelButtonController levelButtonController = levelPackTemp.GetComponent<LevelButtonController>();
                GameObject lockOb = levelPackTemp.transform.Find("Locked").gameObject;
                GameObject unlock = levelPackTemp.transform.Find("Unlcok").gameObject;

                unlock.transform.Find("txtLevelNumber").GetComponent<Text>().text = levelData.levelNumber.ToString();
                // Set lock or unlock
                if (i <= highestLevelSolved )
                {
                    lockOb.SetActive(false);
                    unlock.SetActive(true);
                    //mainImg.color = Color.white;
                    levelButtonController.isLocked = false;
                }
                else
                {
                    unlock.SetActive(false);
                    lockOb.gameObject.SetActive(true);
                    //mainImg.color = lockedColor;
                    levelButtonController.isLocked = true;
                }


                //GridLayoutGroup grlLevelPack = levelPackContent.GetComponent<GridLayoutGroup>();
                //float xSizeOfLevelPackContent = (grlLevelPack.cellSize.x + grlLevelPack.spacing.x) * (packNumber - 1);
                //RectTransform levelPackContentRect = levelPackContent.GetComponent<RectTransform>();
                //levelPackContentRect.sizeDelta = new Vector2(xSizeOfLevelPackContent, levelPackContentRect.sizeDelta.y);
            }
            //Vector2 originSize = levelScrollContent.GetComponent<RectTransform>().sizeDelta;

            ////int itemImageCount = levelScrollContent.transform.childCount;
            //float ScrollRectY = Mathf.Ceil(levelgroupNumber / 3) * ((levelPrefab.GetComponent<RectTransform>().sizeDelta.y)
            //    + levelScrollContent.GetComponent<GridLayoutGroup>().spacing.y) + levelScrollContent.GetComponent<GridLayoutGroup>().padding.top;
            //Debug.Log("ScrollRectY : " + ScrollRectY);
            //levelScrollContent.GetComponent<RectTransform>().sizeDelta = new Vector2(originSize.x, ScrollRectY);

            //levelPackScrollview.GetComponent<BetterScrollview>().DisableInvisibleItems();

            #endregion

            // Check if a new pack has been unlocked
            int oldMaxUnlockedLevel = PlayerPrefs.GetInt(MAX_UNLOCKED_LEVEL_PPK, LEVELS_PER_PACK);  // first pack's levels are unlocked from beginning

            //if (maxUnlockedLevel > oldMaxUnlockedLevel)
            //{
            //    PlayerPrefs.SetInt(MAX_UNLOCKED_LEVEL_PPK, maxUnlockedLevel);
            //}

            //BetterScrollview packScroller = levelPackScrollview.GetComponent<BetterScrollview>();
            //packScroller.SnapToElement((GameManager.LevelLoaded - 1) / LEVELS_PER_PACK);
            //packScroller.DisableInvisibleItems();
        }

        public void ScrollToSelectedPack()
        {
            float levelgroupNumber = Mathf.Ceil(data.Length / (float)buttonGroupPrefab.transform.childCount);

            GridLayoutGroup grlLevelDetailContent = levelDetailContent.GetComponent<GridLayoutGroup>();
            RectTransform levelDetailContentRect = levelDetailContent.GetComponent<RectTransform>();
            levelDetailContentRect = levelDetailContent.GetComponent<RectTransform>();

            // Set content width base on level group number
            float fixedWidth = grlLevelDetailContent.cellSize.x * (levelgroupNumber - 1) + grlLevelDetailContent.spacing.x * levelgroupNumber;
            levelDetailContentRect.sizeDelta = new Vector2(fixedWidth, levelDetailContentRect.sizeDelta.y);

            // Calculate the index of the current pack
            int curPackIndex = Mathf.FloorToInt(levelSnapped / LEVELS_PER_PACK);

            // Calculate the moving distance so that the pack is at screen center
            float difference = curPackIndex * (grlLevelDetailContent.cellSize.x + grlLevelDetailContent.spacing.x);
            levelDetailContentRect.GetComponent<RectTransform>().localPosition = new Vector3(-difference, 0, 0);

            levelDetailScrollview.GetComponent<BetterScrollview>().DisableInvisibleItems();
        }
    }
}
